package cn.yuanqiao.archive.api.util;

import cn.hutool.core.io.IoUtil;
import cn.yuanqiao.archive.appManager.domain.TenantCategory;
import cn.yuanqiao.archive.appManager.domain.TenantCodeList;
import cn.yuanqiao.archive.appManager.domain.TenantCodeRule;
import cn.yuanqiao.archive.appManager.mapper.TenantCategoryMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantCodeListMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantCodeRuleMapper;
import cn.yuanqiao.archive.detection.domain.DetailLogs;
import cn.yuanqiao.archive.detection.domain.DetectionItem;
import cn.yuanqiao.archive.detection.mapper.DetailLogsMapper;
import cn.yuanqiao.archive.detection.mapper.DetectionObjectMapper;
import cn.yuanqiao.archive.detection.service.IDetailLogsService;
import cn.yuanqiao.archive.detection.util.EncryptionUtil;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import com.alibaba.fastjson2.JSONObject;
import com.lowagie.text.pdf.PdfReader;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.sound.sampled.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Component
public class DetectionUtils {

   @Autowired
   private IDetailLogsService detailLogsService;
   @Autowired
   private DetectionObjectMapper detectionObjectMapper;
   @Autowired
   private TenantCategoryMapper tenantCategoryMapper;
   @Autowired
   private DetailLogsMapper detailLogsMapper;
   @Autowired
   private TenantCodeListMapper tenantCodeListMapper;
   @Autowired
   private TenantCodeRuleMapper tenantCodeRuleMapper;

    public  Map getMaps(){
       Map map=new HashMap();
       //各监测项
       //元数据项数项数据长度检测
       map.put("1121","lengthCheck");
       //元数据项数据类型、格式检测
       map.put("1122","typeCheck");
       //设定值域的元数据项值域符合度检测
       map.put("1123","rangeCheck");
       //元数据项数据值合理性检测
       map.put("1124","reasonableCheck");
       //元数据项数据包含特殊字符检测
       map.put("1125","containCheck");
       //档号规范性检测  没写
       map.put("1126","dhCheck");
       //元数据项数据重复性检测
       map.put("1127","repeatCheck");
       //元数据与档案馆要求的一致性检测
       map.put("1128","requireCheck");
       //说明文件和目录文件规范性检测
       map.put("1151","fileNorm");
       //信息包目录结构规范性检测
       map.put("1152","wrapNorm");
       //信息包一致性检测
       map.put("1153","wrapConsistent");
       //电子档案封装包规范性检测
       map.put("1154","packNorm");
       //元数据项完整性检测
       map.put("1221","dataIntegrity");
       //元数据必填著录项目检测
       map.put("1222","requiredIntegrity");
       //过程信息完整性检测
       map.put("1223","msgIntegrity");
       //内容数据完整性检测
       map.put("1231","msgDataIntegrity");
       //归档范围检测
       map.put("1241","scopeCheck");
       //信息包元数据完整性检测
       map.put("1242","gdbysjCheck");
       //信息包中元数据的可读性检测
       map.put("1311","ysjkdxCheck");
       //目标数据库中的元数据可访问性检测
       map.put("1312","ysjkfwCheck");
       //内容数据的可读性检测
       map.put("1322","nrkdsCheck");
       //软硬件环境合规性检测
       map.put("1331","ryjhgxCheck");
       //保存环境变化情况检测
       map.put("1332","bchjbhCheck");
       //备份数据可恢复性检测
       map.put("1351","bfsjkhfCheck");
       //系统环境中是否安装杀毒软件检测
       map.put("1411","azsdrjCheck");
       //病毒感染检测
       map.put("1412","bdgrCheck");
       //载体读取速度检测
       map.put("1422","ztdqsdCheck");
       //载体外观检测
       map.put("1423","ztwgCheck");
       //光盘合格性检测
       map.put("1424","gpheCheck");
       //操作过程安全性检测
       map.put("1431","czgcaqxCheck");
       //软件系统安全漏洞检测
       map.put("1441","aqldCheck");
       //载体保管环境安全性检测
       map.put("1451","bghjaqCheck");
       return map;
    }
   public  Map getFilesMaps(){
      Map map=new HashMap();
      //各监测项
      //固化信息有效性检测
      map.put("1111","hashCheck");
      //内容数据的电子属性一致性检测
      map.put("1131","propertiesCheck");
      //元数据是否关联内容数据检测
      map.put("1141","assCheck");
      //电子档案封装包电子签名有效性检测
      map.put("1155","sigValid");
      //总件数相符性检测
      map.put("1211","numQuantity");
      //总字节数相符性检测
      map.put("1212","byteQuantity");
      //附件数据完整性检测
      map.put("1232","fileIntegrity");
      //信息包内容数据完整性检测
      map.put("1243","xxbwzxCheck");
      //内容数据格式检测
      map.put("1321","nrgsCheck");
      //内容数据格式长期可用性检测
      map.put("1323","cqkdyCheck");
      //信息包中包含的内容数据格式合规性检测
      map.put("1341","brsjgsCheck");
      //载体中多余文件检测
      map.put("1421","dywjCheck");
      return map;
   }
   public  Map getLxMaps(){
       Map map=new HashMap();
      //连续性元数据项检测 没写
      map.put("1224","continuityCheck");
      return map;
   }
    //各个检测项功能 hashCheck 固化信息有效性检测
   public Map<String,Object> hashCheck(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject) throws IOException {
      List<DetailLogs> detailLogs=new ArrayList<>();
      int failCount=0;
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      for(DetectionItem detectionItem:list){
         String val = detectionItem.getValue();
         for(Map<String,String> map:colData) {
            //判断字段存不存在
            String wjPath=map.get("WJPATH");
            if("1".equals(val)){
               //sm3
              if(!map.containsKey("FILE_ONLY_VAL_GM")){
                 failCount++;
                 detailLogs.add(getDetailLogs(sxId,checkType,"固化信息有效性检测不符合,FILE_ONLY_VAL_GM字段不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

              }else{
                String msg="";
                File file=new File(wjPath);
                if(!file.exists()){
                   msg="文件不存在！";
                }else{
                   String sm3Hash= EncryptionUtil.getSM3Hash(wjPath);
                   if(!sm3Hash.equals(map.get("FILE_ONLY_VAL_GM"))){
                      msg="文件"+map.get("DOC_NAME")+"sm3hash值不一致";
                   }
                }
                if(StringUtils.isEmpty(msg)){
                   failCount++;
                  // detailLogsService.insertNewDetailLogs(sxId, "固化信息有效性检测不符合,"+msg, DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                   detailLogs.add(getDetailLogs(sxId,checkType,"固化信息有效性检测不符合,"+msg,map.get("DH")==null?"":map.get("DH"),parentDh,dId));

                }

              }
            }else if("5".equals(val)){
               //md5
               if(!map.containsKey("FILE_ONLY_VAL")){
                  failCount++;
                 // detailLogsService.insertNewDetailLogs(sxId, "固化信息有效性检测不符合,FILE_ONLY_VAL字段不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                  detailLogs.add(getDetailLogs(sxId,checkType,"固化信息有效性检测不符合,FILE_ONLY_VAL字段不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

               }else{
                  String msg="";
                  File file=new File(wjPath);
                  if(!file.exists()){
                     msg="文件不存在！";
                  }else{
                     String sm3Hash= EncryptionUtil.getSM3Hash(wjPath);
                     if(!sm3Hash.equals(map.get("FILE_ONLY_VAL_GM"))){
                        msg="文件"+map.get("DOC_NAME")+"sm3hash值不一致";
                     }
                  }
                  if(StringUtils.isEmpty(msg)){
                     failCount++;
                    // detailLogsService.insertNewDetailLogs(sxId, "固化信息有效性检测不符合,"+msg, DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                     detailLogs.add(getDetailLogs(sxId,checkType,"固化信息有效性检测不符合,"+msg,map.get("DH")==null?"":map.get("DH"),parentDh,dId));

                  }
               }
            }
         }

      }
      Map<String,Object> map=new HashMap<>();
      map.put("failCount",failCount);
      map.put("detailLogs",detailLogs);
      return map;
   }


   //各个检测项功能 元数据项数项数据长度检测
   public Map<String,Object> lengthCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
       if(!map.containsKey(detectionItem.getFieldName())){
          failCount++;
         // detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "元数据项数项数据长度检测不符合设置项！字段不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
          detailLogs.add(getDetailLogs(sxId,checkType,fieldType+"字段" + detectionItem.getFieldCaption() + "元数据项数项数据长度检测不符合设置项！字段不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

          continue;
       }
       String value=map.get(detectionItem.getFieldName());
       if(value.length()>Integer.parseInt(detectionItem.getValue())){
          failCount++;
        //  detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "元数据项数项数据长度不符合设置项", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
          detailLogs.add(getDetailLogs(sxId,checkType, fieldType+"字段" + detectionItem.getFieldCaption() + "元数据项数项数据长度不符合设置项",map.get("DH")==null?"":map.get("DH"),parentDh,dId));
       }
      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //设定值域的元数据项值域符合度检测
   public  Map<String,Object>  typeCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if(!map.containsKey(detectionItem.getFieldName())){
            failCount++;
            //detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "元数据项数据类型、格式检测不符合设置项！字段不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,  fieldType+"字段" + detectionItem.getFieldCaption() + "元数据项数据类型、格式检测不符合设置项！字段不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));
            continue;
         }
         String value=map.get(detectionItem.getFieldName());
         String check=detectionItem.getValue();
         boolean flag=false;
         switch (check) {
            case "0":
               flag=true;
               break;
            case "1":
               flag=verifyNumber(value);
               break;
            case "2":
               flag=checkDataType(value);
               break;
         }
         if(!flag){
            failCount++;
            //新增一条数据
            //detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "元数据项数据类型、格式检测不符合设置项", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,  fieldType+"字段" + detectionItem.getFieldCaption() + "元数据项数据类型、格式检测不符合设置项",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }

   //设定值域的元数据项值域符合度检测
   public Map<String,Object> rangeCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if(!map.containsKey(detectionItem.getFieldName())){
            failCount++;
           // detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "的值不在值域范围内！字段不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,  fieldType+"字段" + detectionItem.getFieldCaption() + "的值不在值域范围内！字段不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));
            continue;
         }
         String value=map.get(detectionItem.getFieldName());
         String check=detectionItem.getValue();
         boolean flag=false;
         String[] arr = check.split(",");
         for (String a : arr) {
         if (value.indexOf(a) >= 0) {
          flag = true;
         }
        }
       if(!flag){
          failCount++;
          //新增一条数据
      //   detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "的值不在值域范围内", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
          detailLogs.add(getDetailLogs(sxId,checkType,  fieldType+"字段" + detectionItem.getFieldCaption() + "的值不在值域范围内",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

       }

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //元数据项数据值合理性检测
   public  Map<String,Object> reasonableCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if(!map.containsKey(detectionItem.getFieldName())){
            failCount++;
            //detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "的值不合理！字段不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,  fieldType+"字段" + detectionItem.getFieldCaption() + "的值不合理！字段不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

            continue;
         }
         String value=map.get(detectionItem.getFieldName());
         String check=detectionItem.getValue();
         boolean flag=false;
         String[] arr = check.split(",");
         for (String a : arr) {
            if (value.indexOf(a) >= 0) {
               flag = true;
            }
         }
         if(!flag){
            failCount++;
            //新增一条数据
          //  detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "的值不合理", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,  fieldType+"字段" + detectionItem.getFieldCaption() + "的值不合理",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //元数据项数据包含特殊字符检测
   public Map<String,Object> containCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if(!map.containsKey(detectionItem.getFieldName())){
            failCount++;
          //  detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   fieldType+"字段" + detectionItem.getFieldCaption() + "不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));
            continue;
         }
         String value=map.get(detectionItem.getFieldName());
         String check=detectionItem.getValue();
         boolean flag = false;
         String[] arr = check.split(",");
         for (String a : arr) {
            if (value.indexOf(a) < 0) {
               flag = true;
            }

         }
         if(!flag){
            failCount++;
            //新增一条数据
           // detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "的值中含有不支持的特殊字符", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   fieldType+"字段" + detectionItem.getFieldCaption() + "的值中含有不支持的特殊字符",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }

   //档号规范性检测
   public Map<String,Object> dhCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      String fieldType="";
      String parentDh="";
      String subTableName="";
      String checkType="";
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      Long categroyId=jsonObject.getLong("categoryId");
      String tenantCode=jsonObject.getString("tenantCode");
      String type="";
      String ntype="";
      TenantCategory category =tenantCategoryMapper.selectTenantCategoryByID(categroyId,tenantCode+TenantCategory.BIGTABLENAME);
      switch (daType) {
         case "0":
            fieldType="组件";
            subTableName=category.getFileTableName();
            checkType="0";
            type="0";
            ntype="f";
            break;
         case "1":
            fieldType="组卷";
            subTableName=category.getFolderTableName();
            checkType="1";
            type="1";
            ntype="v";
            break;
         case "2":
            fieldType="卷内件";
            parentDh=jsonObject.getString("parentDh");
            subTableName=category.getFileTableName();
            checkType="1";
            type="0";
            ntype="v";
            break;
      }
      String finalType = type;
      list = list.stream().filter(item -> item.getType().equals(finalType)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if(!map.containsKey(detectionItem.getFieldName())){
            failCount++;
            //  detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   fieldType+"字段" + detectionItem.getFieldCaption() + "不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

            continue;
         }

         JSONObject json=getdetail(ntype,map.get("RQ")==null?"":map.get("RQ"),categroyId,tenantCode);
         //验证档号规范性
         if(json.containsKey("fail")){
            failCount++;
            //  detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   json.getString("fail"),map.get("DH")==null?"":map.get("DH"),parentDh,dId));
            continue;
         }
         //验证组成项
         String dh="";
         List <String> ziduanarr= (List<String>) json.get("ziduanarr");
         List <String> fuhaoarr = (List<String>) json.get("fuhaoarr");
         List<String> bulingarr= (List<String>) json.get("bulingarr");
         for(int i=0;i<ziduanarr.size();i++){
            String str=ziduanarr.get(i);
            if(!map.containsKey(str)){
               failCount++;
               detailLogs.add(getDetailLogs(sxId,checkType,   "档号组成字段"+str+"不存在！",map.get("DH")==null?"":map.get("DH"),parentDh,dId));
            }else{
               if(i!=ziduanarr.size()-1) {
                  dh += map.get(str)+fuhaoarr.get(i);
               }else {
                int size=Integer.parseInt(bulingarr.get(i))-map.get(str).length();
                for(int j=0;j<size;j++){
                   dh+="0";
                }
                dh+=map.get(str);
               }
            }
         }
         if("2".equals(daType)){
            String jianziduan = json.getString("jianziduan");
            String jianlianjie=json.getString("jianlianjie");
            int jianbuling=json.getInteger("jianbuling");
            if(!map.containsKey(jianziduan)){
               failCount++;
               detailLogs.add(getDetailLogs(sxId,checkType,   "档号组成字段"+jianziduan+"不存在！",map.get("DH")==null?"":map.get("DH"),parentDh,dId));
            }else{
               int size=jianbuling-map.get(jianziduan).length();
               for(int j=0;j<size;j++){
                  dh+="0";
               }
               dh+=jianlianjie+map.get(jianziduan);
            }

         }

         if(!dh.equals(map.get(detectionItem.getFieldName()))){
            detailLogs.add(getDetailLogs(sxId,checkType,   "档号字段"+detectionItem.getFieldCaption()+"不规范！",map.get("DH")==null?"":map.get("DH"),parentDh,dId));
         }
      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //元数据项数据重复性检测
   public Map<String,Object> repeatCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      String fieldType="";
      String parentDh="";
      String subTableName="";
      String checkType="";
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      Long categroyId=jsonObject.getLong("categoryId");
      String tenantCode=jsonObject.getString("tenantCode");

      String type="";
      TenantCategory category =tenantCategoryMapper.selectTenantCategoryByID(categroyId,tenantCode+TenantCategory.BIGTABLENAME);
      switch (daType) {
         case "0":
            fieldType="组件";
            subTableName=category.getFileTableName();
            checkType="0";
            type="0";
            break;
         case "1":
            fieldType="组卷";
            subTableName=category.getFolderTableName();
            checkType="1";
            type="1";
            break;
         case "2":
            fieldType="卷内件";
            parentDh=jsonObject.getString("parentDh");
            subTableName=category.getFileTableName();
            checkType="1";
            type="0";
            break;
      }
      String finalType = type;
      list = list.stream().filter(item -> item.getType().equals(finalType)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if(!map.containsKey(detectionItem.getFieldName())){
            failCount++;
          //  detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldCaption() + "不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   fieldType+"字段" + detectionItem.getFieldCaption() + "不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

            continue;
         }
         String value=map.get(detectionItem.getFieldName());
         int count = checkRepeatability(subTableName, detectionItem.getFieldName(),value,tenantCode);
         if (count > 1) {
            failCount++;
            //detailLogsService.insertNewDetailLogs(sxId, fieldType+"字段" + detectionItem.getFieldName() + "的元数据项数据重复性检测不符合", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   fieldType+"字段" + detectionItem.getFieldCaption() + "的元数据项数据重复性检测不符合",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //元数据与档案馆要求的一致性检测
   public int requireCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
       return 0;
   }
   //内容数据的电子属性一致性检测
   public Map<String,Object> propertiesCheck(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject) throws IOException {
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      for(DetectionItem detectionItem:list){
         String val = detectionItem.getValue();
         for(Map<String,String> map:colData) {
            //判断字段存不存在
            String wjPath=map.get("WJPATH");
            File file=new File(wjPath);
            if("1".equals(val)){
            }else if("2".equals(val)){
             if(!file.exists()){
             failCount++;
            // detailLogsService.insertNewDetailLogs(sxId, "内容数据的电子属性不一致,"  +map.get("DOC_NAME") + "名称不规范,该文件不存在", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                detailLogs.add(getDetailLogs(sxId,checkType,   "内容数据的电子属性不一致,"  +map.get("DOC_NAME") + "名称不规范,该文件不存在",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

             }
            }else if("3".equals(val)){
               String msg="";
               if(!file.exists()){
                  msg=map.get("DOC_NAME") +"文件不存在";
              }else{
                if(map.containsKey("DOC_FILE_SIZE")){
                    if(Long.valueOf(map.get("DOC_FILE_SIZE"))!=file.length()){
                       msg=map.get("DOC_NAME") +"大小不规范";
                    }
              }else{
                   msg="DOC_FILE_SIZE字段不存在";
                }
              }
              if(StringUtils.isNotEmpty(msg)){
                 failCount++;
                // detailLogsService.insertNewDetailLogs(sxId, "内容数据的电子属性不一致,"  +msg, DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                 detailLogs.add(getDetailLogs(sxId,checkType,   "内容数据的电子属性不一致,"  +msg,map.get("DH")==null?"":map.get("DH"),parentDh,dId));

              }
            }else if("4".equals(val)){
               String msg="";
               if(!file.exists()){
                  msg=map.get("DOC_NAME") +"文件不存在!";
               }else{
                  if(map.containsKey("DOC_FILE_TYPE")){
                     if(!file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase().equals(map.get("DOC_FILE_TYPE").toLowerCase())){
                        msg=map.get("DOC_NAME") +"格式不规范!";
                     }
                  }else{
                     msg="DOC_FILE_TYPE字段不存在!";
                  }
               }
              if(StringUtils.isNotEmpty(msg)){
                 failCount++;
                // detailLogsService.insertNewDetailLogs(sxId, "内容数据的电子属性不一致,"  +msg, DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                 detailLogs.add(getDetailLogs(sxId,checkType,   "内容数据的电子属性不一致,"  +msg,map.get("DH")==null?"":map.get("DH"),parentDh,dId));

              }
            }else if("5".equals(val)){
               String msg="";
               if(!file.exists()){
                  msg=map.get("DOC_NAME") +"文件不存在!";
               }else{
                  if(map.containsKey("ACTION_TIME")){
                     // 获取文件创建时间
                     Path path2 = Paths.get(wjPath);
                      // 获取文件属性
                      BasicFileAttributes attributes = Files.readAttributes(path2, BasicFileAttributes.class);
                       // 获取文件创建时间
                      FileTime createTime = attributes.creationTime();
                     // 将文件创建时间转换为标准格式
                      String dateFormat = sdf.format(createTime.toMillis());
                     if (!dateFormat.equals(map.get("ACTION_TIME"))) {
                        msg=map.get("DOC_NAME")+"上传时间不规范!";
                       }
                  }else{
                     msg="ACTION_TIME字段不存在!";
                  }
               }
               if(StringUtils.isNotEmpty(msg)){
                  failCount++;
                 // detailLogsService.insertNewDetailLogs(sxId, "内容数据的电子属性不一致,"  +msg, DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                  detailLogs.add(getDetailLogs(sxId,checkType,  "内容数据的电子属性不一致,"  +msg,map.get("DH")==null?"":map.get("DH"),parentDh,dId));

               }
            }
         }

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //元数据是否关联内容数据检测
   public Map<String,Object> assCheck(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String wjpath=colData.get(0).get("WJPATH");
      String dh="";
      for(Map<String,String> map:colData){
         String path=map.get("WJPATH");
         dh=map.get("DH")==null?"":map.get("DH");
         if(!new File(path).exists()){
            failCount++;
           // detailLogsService.insertNewDetailLogs(sxId, "元数据关联内容数据检测不符合,文件"+map.get("DOC_NAME")+"不存在！"  , DateUtils.getNowDate(), dh, parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,  "元数据关联内容数据检测不符合,文件"+map.get("DOC_NAME")+"不存在！" ,map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }
      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }

   private int checkAss(String folderPath, Set<String> excludeFiles) {
      File folder = new File(folderPath).getParentFile();
      if (!folder.exists() || !folder.isDirectory()) {
         throw new IllegalArgumentException("无效的文件夹路径: " + folderPath);
      }

      int count = 0;
      File[] files = folder.listFiles();

      if (files != null) {
         for (File file : files) {
            if (file.isFile() && !excludeFiles.contains(file.getName())) {
               count++;
            } else if (file.isDirectory()) {
               // 如果需要递归计算子文件夹中的文件数量，可以取消注释以下代码
               // count += countFilesExcept(file.getAbsolutePath(), excludeFiles);
            }
         }
      }

      return count;
   }




   //说明文件和目录文件规范性检测
   public int fileNorm(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;

   }
    //信息包目录结构规范性检测
   public int wrapNorm(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;

   }
   //信息包一致性检测
   public int wrapConsistent(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;

   }
   //电子档案封装包规范性检测
   public int packNorm(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;

   }
   //电子档案封装包电子签名有效性检测
   public int sigValid(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject){
       //需要对接
      return 0;

   }
   //总件数相符性检测
   public int numQuantity(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject){
      return 0;

   }
   //总字节数相符性检测
   public int byteQuantity(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject){
      return 0;

   }
   //元数据项完整性检测
   public  Map<String,Object> dataIntegrity(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if (!map.containsKey(detectionItem.getFieldName())) {
            failCount++;
         //   detailLogsService.insertNewDetailLogs(sxId, "元数据项完整性检测不符合要求,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   "元数据项完整性检测不符合要求,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //元数据必填著录项目检测
   public  Map<String,Object> requiredIntegrity(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if (!map.containsKey(detectionItem.getFieldName())) {
            failCount++;
           // detailLogsService.insertNewDetailLogs(sxId, "元数据必填著录项检测不符合要求,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   "元数据必填著录项检测不符合要求,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }
      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //   //过程信息完整性检测
   public int msgIntegrity(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
       return 0;
   }
   //   //连续性元数据项检测
   public Map<String,Object>  continuityCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      //连续性
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      //String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      List<Map<String,String>> lxList= (List<Map<String, String>>) jsonObject.get("lxList");
      List<Integer> jhList=new ArrayList<>();
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      if(list.size()>0){
         //去验证
         String zd="";
         if("0".equals(type)){
            zd="JH";
         }else{
            zd="AJH";
         }
         Map<String,Object> dhMap=new HashMap<>();
         Map<String,Object> parentDhMap=new HashMap<>();

         for(Map<String,String> map1:lxList){
            if(!map1.containsKey(zd)){
               failCount++;
               //detailLogsService.insertNewDetailLogs(sxId, "元数据必填著录项检测不符合要求,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
               detailLogs.add(getDetailLogs(sxId,checkType,   "连续性元数据检测不通过！"+zd+"不存在！",map1.get("DH")==null?"":map1.get("DH"),map1.get("parentDh")==null?"":map1.get("parentDh"),dId));

            }else{
               if(StringUtils.isEmpty(map1.get(zd))){
                  failCount++;
                  //detailLogsService.insertNewDetailLogs(sxId, "元数据必填著录项检测不符合要求,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                  detailLogs.add(getDetailLogs(sxId,checkType,   "连续性元数据检测不通过！"+zd+"为空！",map1.get("DH")==null?"":map1.get("DH"),map1.get("parentDh")==null?"":map1.get("parentDh"),dId));

               }else {
                  jhList.add(Integer.parseInt(map1.get(zd)));
                  dhMap.put(map1.get(zd), map1.get("DH") == null ? "" : map1.get("DH"));
                  parentDhMap.put(map1.get(zd), map1.get("parentDh") == null ? "" : map1.get("parentDh"));
               }
            }
         }
         if (jhList.size() > 1) {
            Object[] array = jhList.toArray();
            Arrays.sort(array);
            int index = Integer.parseInt(array[0].toString());
            for (int i = 1; i < array.length; i++) {
               if ((Integer.parseInt(array[i].toString()) - index) != 1) {
                  failCount++;

                  String message = "";
                  if ("1".equals(checkType)) {
                     message = "案卷号";
                  } else if ("0".equals(checkType)) {
                     message = "件号";

                  }
                  detailLogs.add(getDetailLogs(sxId,checkType,   message + index + "," + Integer.parseInt(array[i].toString()) + "之间不连续",String.valueOf(dhMap.get(array[i])==null?"":dhMap.get(array[i])),String.valueOf(parentDhMap.get(array[i])==null?"":parentDhMap.get(array[i])),dId));
                  index = Integer.parseInt(array[i].toString());

               } else {
                  index = Integer.parseInt(array[i].toString());
               }
            }
         }
      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //内容数据完整性检测
   public int msgDataIntegrity(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;

      return failCount;
   }
//   //附件数据完整性检测
   public int fileIntegrity(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject){
       return 0;
   }
   //   //归档范围检测
   public int scopeCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      return 0;
   }
     //信息包元数据完整性检测
   public  Map<String,Object> gdbysjCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if (!map.containsKey(detectionItem.getFieldName())) {
            failCount++;
            //detailLogsService.insertNewDetailLogs(sxId, "信息包元数据完整性检测不符合！,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   "信息包元数据完整性检测不符合！,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！",map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }

   //电子件处理
   public Map<String,Object>  xxbwzxCheck(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject){
      //判断文件是否存在+判断文件数量是否符合
      boolean flag=true;
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String wjpath=colData.get(0).get("WJPATH");
      String dh="";
      for(Map<String,String> map:colData){
         String path=map.get("WJPATH");
         dh=map.get("DH")==null?"":map.get("DH");
         if(!new File(path).exists()){
            flag=false;
         }

      }
      boolean b=true;
      if(colData.size()>0){
         Set<String> excludeFiles = new HashSet<>();
       //  excludeFiles.add("project.xml");
        // excludeFiles.add("metadata.xml");
         excludeFiles.add("说明文件.xml");
         // 调用方法计算文件总数
         int fileCount = checkAss(wjpath, excludeFiles);
         //减去里面的xml文件
         if((fileCount-1)!=colData.size()){
            b=false;
         }
      }
      if(!flag||!b){
         failCount++;
        // detailLogsService.insertNewDetailLogs(sxId, "信息包内容数据完整性检测不通过,"  , DateUtils.getNowDate(), dh, parentDh, checkType, dId);
         detailLogs.add(getDetailLogs(sxId,checkType,    "信息包内容数据完整性检测不通过" ,dh,parentDh,dId));

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
    }
   public Map<String,Object> ysjkdxCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if (!map.containsKey(detectionItem.getFieldName())) {
            failCount++;
           // detailLogsService.insertNewDetailLogs(sxId, "信息包中元数据的可读性不符合！,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,     "信息包中元数据的可读性不符合！,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！" , map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }
     }
     // return failCount;
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }

   public Map<String,Object> ysjkfwCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String type=json.getString("type");
      list = list.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
      for(DetectionItem detectionItem:list){
         if (!map.containsKey(detectionItem.getFieldName())) {
            failCount++;
            //detailLogsService.insertNewDetailLogs(sxId, "目标数据库中的元数据可访问性检测不符合！,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！", DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,     "目标数据库中的元数据可访问性检测不符合！,"+fieldType+"字段" + detectionItem.getFieldCaption() + "字段不存在！" , map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }
      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
  //   //内容数据格式检测 电子件
   public Map<String,Object>  nrgsCheck(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject){
       int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String wjpath=colData.get(0).get("WJPATH");
      String name1 = "";
      for (DetectionItem detectionItem : list) {
         String name = detectionItem.getName();
         if (name.equals("word")) {
            name1 += "doc,docx,";
         } else if (name.equals("excel")) {
            name1 += "xls,xlsx,";
         } else {
            name1 += name + ",";
         }
      }
      if (!StringUtils.isEmpty(name1)) {
         for (Map<String,String> map : colData) {
            if(map.containsKey("DOC_FILE_TYPE")) {
               if (name1.indexOf(map.get("DOC_FILE_TYPE").toLowerCase() + ",") < 0) {
                  failCount++;
                  //detailLogsService.insertNewDetailLogs(sxId, "内容数据格式检测不包含,"  +map.get("DOC_FILE_TYPE").toLowerCase(), DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                  detailLogs.add(getDetailLogs(sxId,checkType, "内容数据格式检测不包含,"  +map.get("DOC_FILE_TYPE").toLowerCase() , map.get("DH")==null?"":map.get("DH"),parentDh,dId));

               }
            }
         }
      }

      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
    }
   //内容数据的可读性检测
   public int nrkdsCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      return 0;
   }
   ////内容数据格式长期可用性检测 电子件
   public Map<String,Object> cqkdyCheck(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject){
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String wjpath=colData.get(0).get("WJPATH");
      String name1 = "";
      for (DetectionItem detectionItem : list) {
         String name = detectionItem.getName();
         if (name.equals("word")) {
            name1 += "doc,docx,";
         } else if (name.equals("excel")) {
            name1 += "xls,xlsx,";
         } else {
            name1 += name + ",";
         }
      }
      if (!StringUtils.isEmpty(name1)) {
         for (Map<String,String> map : colData) {
            if(map.containsKey("DOC_FILE_TYPE")) {
               if (name1.indexOf(map.get("DOC_FILE_TYPE").toLowerCase() + ",") < 0) {
                  failCount++;
                 // detailLogsService.insertNewDetailLogs(sxId, "长期可用性检测不包含,"  +map.get("DOC_FILE_TYPE").toLowerCase(), DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
                  detailLogs.add(getDetailLogs(sxId,checkType,  "长期可用性检测不包含,"  +map.get("DOC_FILE_TYPE").toLowerCase() , map.get("DH")==null?"":map.get("DH"),parentDh,dId));

               }
            }
         }
      }

      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   public int ryjhgxCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      return 0;
   }
   public int bchjbhCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      return 0;
   }
   //信息包中包含的内容数据格式合规性检测
   public Map<String,Object> brsjgsCheck(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject) throws IOException {
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();

      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String wjpath=colData.get(0).get("WJPATH");
      String name1 = "";
      for (Map<String,String> map : colData) {
         String path=map.get("WJPATH");
         File file=new File(path);
         String msg="";
         if(!file.exists()){
            msg=map.get("DOC_NAME")+"文件不存在！";
         }else{
            boolean flag=IsEncryption(wjpath,wjpath.substring(wjpath.lastIndexOf(".") + 1));
            if(flag){
              msg=map.get("DOC_NAME")+"文件加密！";
            }
         }
         if(StringUtils.isNotEmpty(msg)){

           // detailLogsService.insertNewDetailLogs(sxId, "信息包中包含的内容数据格式合规性检测不通过"  +msg, DateUtils.getNowDate(), map.get("DH")==null?"":map.get("DH"), parentDh, checkType, dId);
            detailLogs.add(getDetailLogs(sxId,checkType,   "信息包中包含的内容数据格式合规性检测不通过"  +msg, map.get("DH")==null?"":map.get("DH"),parentDh,dId));

         }

      }

      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   //备份数据可恢复性检测
   public int bfsjkhfCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
       //未对接
      return 0;
   }
   //系统环境中是否安装杀毒软件检测
   public int azsdrjCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      //未对接
      return 0;
   }
   //病毒感染检测
   public int bdgrCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){
      //未对接
      return 0;
   }
   //载体多余检测
   public Map<String,Object>  dywjCheck(List<Map<String, String>> colData, List<DetectionItem> list, JSONObject jsonObject){
      //判断文件是否存在+判断文件数量是否符合
      boolean flag=true;
      int failCount=0;
      List<DetailLogs> detailLogs=new ArrayList<>();
      Long sxId=jsonObject.getLong("sxId");
      Long dId=jsonObject.getLong("dId");
      String daType=jsonObject.getString("daType");
      JSONObject json=getJson(daType,jsonObject);
      String fieldType=json.getString("fieldType");
      String parentDh=json.getString("parentDh");
      String checkType=json.getString("checkType");
      String wjpath=colData.get(0).get("WJPATH");
      String dh="";
      for(Map<String,String> map:colData){
         String path=map.get("WJPATH");
         dh=map.get("DH")==null?"":map.get("DH");
         if(!new File(path).exists()){
            flag=false;
         }

      }
      boolean b=true;
      if(colData.size()>0){
         Set<String> excludeFiles = new HashSet<>();
         excludeFiles.add("说明文件.xml");
         // 调用方法计算文件总数
         int fileCount = checkAss(wjpath, excludeFiles);
         //减去里面的xml文件
         if((fileCount-1)!=colData.size()){
            b=false;
         }
      }
      if(!flag||!b){
         failCount++;
        // detailLogsService.insertNewDetailLogs(sxId, "载体中多余文件检测文件数目不一致,"  , DateUtils.getNowDate(), dh, parentDh, checkType, dId);
         detailLogs.add(getDetailLogs(sxId,checkType,    "载体中多余文件检测文件数目不一致,", dh,parentDh,dId));

      }
      Map<String,Object> maps=new HashMap<>();
      maps.put("failCount",failCount);
      maps.put("detailLogs",detailLogs);
      return maps;
   }
   public int ztdqsdCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;
   }
   public int ztwgCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;
   }
   public int gpheCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;
   }
   public int czgcaqxCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;
   }
   public int aqldCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;
   }
   public int bghjaqCheck(Map<String, String> map, List<DetectionItem> list, JSONObject jsonObject){

      return 0;
   }

   public boolean verifyNumber(String value) {

      String reg = "^-?[0-9]+(.[0-9]+)?$";
      return value.matches(reg);
   }
   public boolean checkDataType(Object object) {
      boolean flag = true;
      Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\.\\-\\/\\s]?((((0?[13578])|(1[02]))[\\.\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\.\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\.\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\.\\-\\/\\s]?((((0?[13578])|(1[02]))[\\.\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\.\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\.\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
      flag = p.matcher(object.toString()).matches();
      return flag;
   }
   public int checkRepeatability(String subTableName, String fieldName, String value,String tenantCode) {

      return detectionObjectMapper.getCountByDetection(subTableName, fieldName, tenantCode.toUpperCase(), value);
   }

   public JSONObject getJson(String daType,JSONObject jsonObject){
       JSONObject json=new JSONObject();
       String type="";
       String fieldType="";
       String checkType="";
       String parentDh="";
       switch (daType) {
         case "0":
            fieldType="组件";
            checkType="0";
            type="0";
            break;
         case "1":
            fieldType="组卷";
            checkType="1";
            type="1";
            break;
         case "2":
            fieldType="卷内件";
            checkType="1";
            parentDh=jsonObject.getString("parentDh");
            type="0";
            break;
      }
      json.put("fieldType",fieldType);
      json.put("checkType",checkType);
      json.put("parentDh",parentDh);
      json.put("type",type);
      return json;


   }
   //判断
   public  boolean IsEncryption(String file,String fileType) throws IOException {
      if(fileType.toUpperCase().equals("PDF")){
         return PdfIsEncryption(file);
      }else if (fileType.toUpperCase().equals("JPG")||fileType.toUpperCase().equals("PNG")){
         return isImageEncrypted(file);
      }else if (fileType.toUpperCase().equals("XLS")||fileType.toUpperCase().equals("XLSX")){
         return isExcelEncrypted(file);
      }else if (fileType.toUpperCase().equals("PPT")){
         return isPptProtected(file);
      }else if (fileType.toUpperCase().equals("DOC")||fileType.toUpperCase().equals("DOCX")){
         return isWordEncrypted(file);
      }else if (fileType.toUpperCase().equals("MP3")){
         return isMP3Decodable(file);
      }else{
         return false;
      }

   }

   //判断pdf是否加密
   public  Boolean PdfIsEncryption(String file){
      PdfReader reader = null;
      try {
         reader = new PdfReader(file);
         return reader.isEncrypted();
      }catch (Exception e){
         e.printStackTrace();
         return true;
      }finally {
         if(reader !=null){
            reader.close();
         }
      }
      // return true;
   }


   //图片是否加解密
   public  Boolean  isImageEncrypted(String imagePath) {
      try {
         BufferedImage image = ImageIO.read(new File(imagePath));
         return image == null;
      } catch (IOException e) {
         // 解码过程中发生了异常，可能图片已加密或文件损坏
         return true;
      }
   }

   //判断excel是否加密
   public  boolean isExcelEncrypted(String filePath) {
      try {
         WorkbookFactory.create(new File(filePath));
         // 如果上面的代码没有抛出异常，则文件不加密
         return false;
      } catch (Exception e) {
         // 如果抛出EncryptedDocumentException，则文件加密
         return true;
      }
   }

   public  boolean isWordEncrypted(String filePath) {
      boolean isProtected = false;
      WordExtractor extractor = null;
      try (FileInputStream fis = new FileInputStream(new File(filePath))) {
         // 创建WordExtractor以读取内容，这一步可能会在文件受保护时失败
         extractor = new WordExtractor(fis);
         extractor.close(); // 关闭提取器
      } catch (EncryptedDocumentException e){
         isProtected = true;
      } catch (IOException e) {
         if(e.getMessage().indexOf("EncryptedPackage") != -1){
            isProtected = true;
         }
      } finally {
         if(extractor!=null){
            IoUtil.close(extractor);
         }
         return isProtected;
      }

   }
   public  boolean isPptProtected(String path) {
      File file=new File(path);
      boolean isProtected = false;
      HSLFSlideShow ppt=null;
      try (FileInputStream fis = new FileInputStream(file)) {
         ppt = new HSLFSlideShow(fis);
         ppt.getPageSize();
         ppt.close();
      } catch (Exception e){
         if(e.getMessage().toLowerCase().indexOf("encrypted")!=-1){
            isProtected = true;
         }
      } finally {
         if(ppt!=null){
            IoUtil.close(ppt);
         }
         return isProtected;
      }
   }


   public   boolean isMP3Decodable(String path) {
      try {
         File mp3File=new File(path);
         AudioFormat format = AudioSystem.getAudioFileFormat(mp3File).getFormat();
         SourceDataLine line = AudioSystem.getSourceDataLine(format);
         line.open(format);
         line.start();
         line.close();
         return true;
      } catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
         return false;
      }
   }

   public String readTxtFile(String filePath) {
      StringBuffer result = new StringBuffer();
      try {
         String encoding = "UTF-8";
         File file = new File(filePath);
         if (file.isFile() && file.exists()) { // 判断文件是否存在
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); // 考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
               result.append(lineTxt);
            }
            read.close();
         } else {
            System.out.println("找不到指定的文件");
         }
      } catch (Exception e) {
         System.out.println("读取文件内容出错");
         e.printStackTrace();
      }
      return result.toString();
   }
   public DetailLogs getDetailLogs(Long sxId,String checkType,String mes,String checkDh,String parentDh,Long dId){
      DetailLogs detailLogs=new DetailLogs();
      detailLogs.setId(SnowflakeIdGenerator.generateId());
      detailLogs.setpId(sxId);
      detailLogs.setDetectionTime(DateUtils.getNowDate());
      detailLogs.setCheckType(checkType);
      detailLogs.setMessage(mes);
      detailLogs.setCheckDh( checkDh);
      detailLogs.setCheckParentDh(parentDh);
      detailLogs.setdId(dId);
     return detailLogs;
   }

   //获取档号组成字段

   public JSONObject getdetail(String type,String rq,Long categoryId,String tenantCode) {
      //获取档号规则
      List<Map> list1 = new ArrayList<>();
      if (cn.yuanqiao.common.utils.StringUtils.isNotEmpty(rq)) {
         rq = rq.replaceAll("-", "").replaceAll("/", "");
         rq = addZero(rq);
      }
      if (!cn.yuanqiao.common.utils.StringUtils.isEmpty(rq)) {
         list1 = detectionObjectMapper.getDhByRq(rq, tenantCode.toUpperCase(), categoryId);
      }
      if (list1.size() <= 0) {
         list1 = detectionObjectMapper.getDh(rq, tenantCode.toUpperCase(), categoryId);
      }
      JSONObject json=new JSONObject();
      if(list1.size()<=0){
         json.put("fail", "该档案不存在档号规则！");

      }else {
         TenantCodeList yqdaCodelistModel = new TenantCodeList();
         yqdaCodelistModel.setTableName(tenantCode + TenantCodeList.BIGTABLENAME);
         yqdaCodelistModel.setArchivalcodeRuleId(Long.parseLong(list1.get(0).get("RULEID").toString()));
         List<TenantCodeList> yqdaCodelistModels = tenantCodeListMapper.selectYqdaCodelistModelList(yqdaCodelistModel);
         List<String> ziduanarr = new ArrayList<>();
         List<String> fuhaoarr = new ArrayList<>();
         List<String> bulingarr = new ArrayList<>();
         for (int i = 0; i < yqdaCodelistModels.size(); i++) {
            if (yqdaCodelistModels.get(i).getVFETYPE().equals(type) && "著录项值".equals(yqdaCodelistModels.get(i).getType())) {//件
               ziduanarr.add(yqdaCodelistModels.get(i).getNodeValue());
               bulingarr.add(String.valueOf(yqdaCodelistModels.get(i).getLen()));
            } else if (yqdaCodelistModels.get(i).getVFETYPE().equals(type) && "固定值".equals(yqdaCodelistModels.get(i).getType())) { //卷 或 项目
               fuhaoarr.add(yqdaCodelistModels.get(i).getNodeValue());
            } else if ("e".equals(yqdaCodelistModels.get(i).getVFETYPE())) {
               json.put("filelianjie", yqdaCodelistModels.get(i).getNodeValue());
            } else {
               if ("著录项值".equals(yqdaCodelistModels.get(i).getType())) {
                  json.put("jianziduan", yqdaCodelistModels.get(i).getNodeValue());
                  json.put("jianbuling", yqdaCodelistModels.get(i).getLen());
               } else {
                  json.put("jianlianjie", yqdaCodelistModels.get(i).getNodeValue());
               }
            }
         }
         json.put("ziduanarr", ziduanarr);
         json.put("fuhaoarr", fuhaoarr);
         json.put("bulingarr", bulingarr);
         TenantCodeRule yqdaCoderuleModel = tenantCodeRuleMapper.selectYqdaCoderuleModelById(Long.parseLong(list1.get(0).get("RULEID").toString()), tenantCode + TenantCodeRule.BIGTABLENAME);
         json.put("columnname", "f".equals(type) ? yqdaCoderuleModel.getfColumnname() : yqdaCoderuleModel.getvColumnname());
         json.put("jiancunchuziduan", yqdaCoderuleModel.getfColumnname());
         json.put("order", yqdaCoderuleModel.getOrderStr());
         json.put("filecunchuziduan", yqdaCoderuleModel.geteColumnname());
      }
      return json;
   }
   //字符串不足8位末尾补零
   public String addZero(String str) {
      int length = str.length();
      if (length < 8) {
         int b = 8 - length;
         for (int i = 0; i < b; i++) {
            str += "0";
         }
         return str;
      }
      return str;
   }
}
